#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>

int main()
{
 
  int fd = open("myfile.txt", O_CREAT | O_WRONLY , 0644);
  if(fd < 0)
  {
    perror("open");
    return 1;
  }

  dup2(fd, 1);
  

  //向显示器输出 就是像文件里写
  printf("try printf\n");
  fflush(stdout);




  //close(1);
  //int fd = open("myfile.txt", O_CREAT | O_WRONLY | O_TRUNC, 0644);
  //if(fd < 0)
  //{
  //  perror("open");
  //}
  //
  //printf("fd: %d\n", fd);

  //const char* buf = "asdasda";
  //write(fd, buf, sizeof(buf));
  


  //char buf[1024];
  //size_t s = read(0,buf,sizeof(buf));
  //if(s > 0)
  //{
  //  //buf[s] = 0;
  //  write(1, buf, strlen(buf));
  //  write(2, buf, strlen(buf));
  //}

  


  //umask(0);
  //int fd1 = open("log1.txt", O_CREAT | O_WRONLY | O_TRUNC, 0666);
  //int fd2 = open("log2.txt", O_CREAT | O_WRONLY | O_TRUNC, 0666);
  //int fd3 = open("log3.txt", O_CREAT | O_WRONLY | O_TRUNC, 0666);
  //int fd4 = open("log4.txt", O_CREAT | O_WRONLY | O_TRUNC, 0666);
  //
  //if(fd1 < 0) exit(1);
  //if(fd2 < 0) exit(1);
  //if(fd3 < 0) exit(1);
  //if(fd4 < 0) exit(1);
  //
  //printf("fd1: %d\n", fd1);
  //printf("fd2: %d\n", fd2);
  //printf("fd3: %d\n", fd3);
  //printf("fd4: %d\n", fd4);
  //
  //close(fd1);
  //close(fd2);
  //close(fd3);
  //close(fd4);
  //return 0;
}
